using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using Microsoft.Win32;

namespace IEContextInstallerClass
{
    [RunInstaller(true)]
    public partial class CaptureLink : Installer
    {
        public CaptureLink()
        {
            InitializeComponent();
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            //grab the target director and add to install.installstate
            stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString());
        }
        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState);

            //adding items to the context menu of IE
            RegistryKey key;
            string keyValueInt = "16";
            string subKey = @"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Capture Link";
            key = Registry.CurrentUser.CreateSubKey(subKey);
            key.SetValue("Contexts", Convert.ToInt32(keyValueInt), RegistryValueKind.DWord);
            key.SetValue(null, "file://" + savedState["TargetDir"].ToString() + "\\action.html");
            key.Close();

            //Create a Reg Key for CaptureLink EXE
            RegistryKey keyApp;
            string subKeyApp = @"SOFTWARE\CaptureLink";
            keyApp = Registry.CurrentUser.CreateSubKey(subKeyApp);

            //////For normalizing the Target directory Path
            string strTargetDir = savedState["TargetDir"].ToString();
            strTargetDir = strTargetDir.Replace("\\","\\\\");
            //keyApp.SetValue(null, savedState["TargetDir"].ToString() + "\\CaptureLinks.exe");
            //keyApp.SetValue(null, strTargetDir + "CaptureLinks.exe");
            keyApp.SetValue(null, strTargetDir);
            
            keyApp.Close();

        }
        protected override void OnBeforeUninstall(System.Collections.IDictionary savedState)
        {
            base.OnBeforeUninstall(savedState);
            //removing items from the context menu of IE
            string subKey = @"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Capture Link";
            Registry.CurrentUser.DeleteSubKey(subKey);

            string subKeyApp = @"SOFTWARE\CaptureLink";
            Registry.CurrentUser.DeleteSubKey(subKeyApp);
        }
    }
}